
function __getClass(value): string {
    return Object.prototype.toString.call(value).match(/^\[object\s(.*)\]$/)[1];
}

function isNumber(value): boolean {
    return __getClass(value) == "Number";
}
function isString(value): boolean {
    return __getClass(value) == "String";
}

export function getStringArrayFromStr(str: string): Array<string> {
    if (str.length <= 0) {
        return [];
    }
    let lines = str.split('\\n');
    return lines;
}

export function getNumberArrayFromStr(str: string): Array<number> {
    if (str.length <= 0) {
        return [];
    }

    let lines = str.split('\\n');
    let ret = new Array<number>();
    for (let i of lines) {
        ret.push(parseInt(i));
    }

    return ret;
}

export function formatTime(leftTimeSec: number): string {
    let ret = '';
    if (leftTimeSec > 3600) {
        //超过1小时
        ret = Math.floor(leftTimeSec / 3600) + 'h' + Math.floor(leftTimeSec % 3600 / 60) + 'm';
    }
    else if (leftTimeSec > 60) {
        //超过60秒
        ret = Math.floor(leftTimeSec / 60) + 'm';
    }
    else {
        ret = leftTimeSec + 's';
    }

    return ret;
}


/*
 *@desc：clone 数据,深拷贝
 *@param originObj 原始对象
*/

export function clone(originObj) {
    let result;
    if (typeof originObj == "object") {
        if (originObj === null) {
            result = null;
        }
        else {
            if (originObj instanceof Array) {
                result = [];
                for (let i = 0, len = originObj.length; i < len; i++) {
                    result.push(clone(originObj[i]));
                }
            }
            else if (originObj instanceof Map) {
                result = new Map;
                for (let [key, value] of originObj) {
                    result.set(clone(key), clone(value));
                }
            }
            else {
                result = {};
                for (let j in originObj) {
                    result[j] = clone(originObj[j]);
                }
            }
        }
    }
    else {
        result = originObj;
    }
    return result;
}



//兼容配置表里的结构体数组，把字符串转换成结构体数组
export function getStructArrayFromStr<T>(str: string, type: new () => T): Array<T> {
    if (str.length <= 0) {
        return [];
    }
    let ret = new Array<T>();
    let lines = str.split('\\n');
    for (let i of lines) {
        let tempRet: T = new type();
        let items = i.split(',');

        let tempkeys = Reflect.ownKeys(Object(tempRet));
        if (tempkeys.length <= 0) {
            tempkeys = Reflect.ownKeys(Object(tempRet.__proto__));
        }

        let keys = [];
        for (let oneKey of tempkeys) {
            if (oneKey == 'constructor' || oneKey == 'toJSON') {
                continue
            }

            keys.push(oneKey);
        }

        if (items.length != keys.length) {
            return;
        }

        let k = 0;
        for (let p of keys) {
            let valueType: string = __getClass(tempRet[p]);
            switch (valueType) {
                case "Number":
                    tempRet[p] = parseInt(items[k]);
                    break;
                default:
                    tempRet[p] = items[k];
                    break;
            }
            k++;
        }
        ret.push(<T>(tempRet));
    }
    return ret;
}

/**
    获取随机整数
    @param min 随机的最小值
    @param max 随机的最大值(不包括该值)
    @returns 返回一个整数，范围是 [min, max)
*/
export function getRandomInt(min: number, max: number): number {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}

/*
 *去掉文件的后缀名 "ui/icon/head.png" --> "ui/icon/head" 
 *  
*/

export function removeFileSuffix(file: string) {
    if (!file) {
        return null;
    }
    let arr = file.split('.');
    let ret = '';

    if (arr.length >= 2) {
        for (let idx = 0; idx < arr.length - 1; ++idx) {
            ret += arr[idx];
        }
    }
    else {
        ret = arr[0];
    }

    return ret;
}

export function StringToUint8Array(str) {
    var arr = [];
    for (var i = 0, j = str.length; i < j; ++i) {
        arr.push(str.charCodeAt(i));
    }

    var tmpUint8Array = new Uint8Array(arr);
    return tmpUint8Array
}

export function Uint8ArrayToString(fileData) {
    var dataString = "";
    for (var i = 0; i < fileData.length; i++) {
        dataString += String.fromCharCode(fileData[i]);
    }

    return dataString

}